/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.manchester.biology.eqhei.servlet;

import edu.manchester.biology.eqhei.implementations.eQHEI_AttributeIMP;
import edu.manchester.biology.eqhei.implementations.eQHEI_AttributeSetIMP;
import edu.manchester.biology.eqhei.implementations.eQHEI_EvaluationIMP;
import edu.manchester.biology.eqhei.implementations.eQHEI_HeaderIMP;
import edu.manchester.biology.eqhei.implementations.eQHEI_SectionIMP;
import edu.manchester.biology.eqhei.implementations.eQHEI_StreamReachIMP;
import edu.manchester.biology.eqhei.implementations.eQHEI_UTMIMP;
import edu.manchester.biology.eqhei.interfaces.eQHEI_AttributeSet;
import edu.manchester.biology.eqhei.interfaces.eQHEI_Attribute;
import edu.manchester.biology.eqhei.interfaces.eQHEI_Evaluation;
import edu.manchester.biology.eqhei.interfaces.eQHEI_Header;
import edu.manchester.biology.eqhei.interfaces.eQHEI_Section;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Michael Kendall
 */
public class eQHEI_Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //envirnoment setup
        System.out.println("servlet started");
        HttpSession session = request.getSession(true);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.jsp");
        
        //content setup
        eQHEI_Evaluation e = null;
        eQHEI_Header h = null;
        try {
            e = getEvaluation();
            session.setAttribute("Eval", e);
        } catch (Exception ex) {
            System.out.println("Inside exception " + ex.getMessage());
            Logger.getLogger(eQHEI_Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            h = getHeader();
            session.setAttribute("Header", h);
        } catch (Exception ex) {
            System.out.println("Inside exception " + ex.getMessage());
            Logger.getLogger(eQHEI_Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //forward
        requestDispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        eQHEI_Evaluation e;
       
        eQHEI_Header h = null;
        System.out.println("doPost");
        try{
            e = sendEvaluation(request);
            System.out.println("After sendEvaluation()");
            System.out.println(e.length());
            for(int i = 0; i<e.length(); i++){
                eQHEI_Section s = e.get(i);
                System.out.println("Section Name: " + s.getName());
                for(int j = 0; j<s.length(); j++){
                    eQHEI_AttributeSet as = s.get(j);
                    System.out.println("AttibuteSet Name: " + as.getName());
                    for(int k = 0; k<as.length(); k++){
                        eQHEI_Attribute a = as.get(k);
                        System.out.println("Attribute Name: " + a.getName() + " Value: " + a.getCount());
                    }
                }
            }
        }
        catch(Exception ex){
            System.out.println("doPost " + ex.getMessage());
            Logger.getLogger(eQHEI_Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try{
            h = sendHeader(request);
            System.out.println("Date: " + h.getDate());
            System.out.println("Username: " + h.getUsername());
            System.out.println("UTM: " + h.getUTM());
            System.out.println("Stream Reach: " + h.getStreamReach());
        }
        catch(Exception ex){
            System.out.println("doPost " + ex.getMessage());
            Logger.getLogger(eQHEI_Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    private eQHEI_Evaluation getEvaluation() throws Exception{
        //Creating the Evaluation
        eQHEI_Evaluation e = new eQHEI_EvaluationIMP();
        
        //Creating the Sections
        eQHEI_Section s = new eQHEI_SectionIMP("Substrates");
        eQHEI_Section s1 = new eQHEI_SectionIMP("Instream Cover");
        eQHEI_Section s2 = new eQHEI_SectionIMP("Channel Morphology");
        eQHEI_Section s3 = new eQHEI_SectionIMP("Bank Erosion and Riparian Zone");
        eQHEI_Section s4 = new eQHEI_SectionIMP("Pool / Glide");
        eQHEI_Section s5 = new eQHEI_SectionIMP("Riffle / Run Quality");
        eQHEI_Section s6 = new eQHEI_SectionIMP("Gradient");
        
        //Adding the sections to the Evaluation
        e.add(s);
        e.add(s1);
        e.add(s2);
        e.add(s3);
        e.add(s4);
        e.add(s5);
        e.add(s6);
        
        //Section s Attribute Sets
        s.add(new eQHEI_AttributeSetIMP("Best Types"));
        s.add(new eQHEI_AttributeSetIMP("Other Types"));
        s.add(new eQHEI_AttributeSetIMP("Origin"));
        s.add(new eQHEI_AttributeSetIMP("Silt Quality"));
        s.add(new eQHEI_AttributeSetIMP("Embeddedness Quality"));
                
                //Creating attibutes for the Attribute Sets
                eQHEI_AttributeSet as = s.get("Best Types");
                
                //Attribute Set Best Types Attributes
                as.add(new eQHEI_AttributeIMP("BLDR/SLABS", 10));
                as.add(new eQHEI_AttributeIMP("BOULDER", 9));
                as.add(new eQHEI_AttributeIMP("COBBLE", 8));
                as.add(new eQHEI_AttributeIMP("GRAVEL", 7));
                as.add(new eQHEI_AttributeIMP("SAND", 6));
                as.add(new eQHEI_AttributeIMP("BEDROCK", 5));
                
                //Attribute Set Other Types Attributes
                as = s.get("Other Types");
                as.add(new eQHEI_AttributeIMP("HARDPAN", 4));
                as.add(new eQHEI_AttributeIMP("DETRITUS", 3));
                as.add(new eQHEI_AttributeIMP("MUCK", 2));
                as.add(new eQHEI_AttributeIMP("SILT", 2));
                as.add(new eQHEI_AttributeIMP("ARTIFICIAL", 0));
                
                //Attribute Set Origin Types Attributes
                as = s.get("Origin");
                as.add(new eQHEI_AttributeIMP("LIMESTONE", 1));
                as.add(new eQHEI_AttributeIMP("TILLS", 1));
                as.add(new eQHEI_AttributeIMP("WETLANDS", 0));
                as.add(new eQHEI_AttributeIMP("HARDPAN", 0));
                as.add(new eQHEI_AttributeIMP("SANDSTONE", 0));
                as.add(new eQHEI_AttributeIMP("RIP/RAP", 0));
                as.add(new eQHEI_AttributeIMP("LACUSTURINE", 0));
                as.add(new eQHEI_AttributeIMP("SHALE", -1));
                as.add(new eQHEI_AttributeIMP("SHALE", -2));
                
                //Attribute Set Silt Quality Attributes
                as = s.get("Silt Quality");
                as.add(new eQHEI_AttributeIMP("HEAVY", -2));
                as.add(new eQHEI_AttributeIMP("MODERATE", -1));
                as.add(new eQHEI_AttributeIMP("NORMAL", -1));
                as.add(new eQHEI_AttributeIMP("FREE", 1));
                
                //Attribute Set Embeddedness Quality Attributes
                as = s.get("Embeddedness Quality");
                as.add(new eQHEI_AttributeIMP("EXTENSIVE", -2));
                as.add(new eQHEI_AttributeIMP("MODERATE", -1));
                as.add(new eQHEI_AttributeIMP("NORMAL", 0));
                as.add(new eQHEI_AttributeIMP("NONE", 1));
        
        //Section s1 Attribute Sets
        s1.add(new eQHEI_AttributeSetIMP("Type"));
        s1.add(new eQHEI_AttributeSetIMP("Amount"));
                
                //Attribute Set Type Attributes
                as = s1.get("Type");
                as.add(new eQHEI_AttributeIMP("UNDERCUT BANKS", 1));
                as.add(new eQHEI_AttributeIMP("OVERHANGING VEGETATION", 1));
                as.add(new eQHEI_AttributeIMP("SHALLOWS (IN SLOW WATER)", 1));
                as.add(new eQHEI_AttributeIMP("ROOTMATS", 1));
                as.add(new eQHEI_AttributeIMP("POOLS > 70cm", 2));
                as.add(new eQHEI_AttributeIMP("ROOTWADS", 1));
                as.add(new eQHEI_AttributeIMP("BOULDERS", 1));
                as.add(new eQHEI_AttributeIMP("OXBOWS, BACKWATERS", 1));
                as.add(new eQHEI_AttributeIMP("AQUATIC MACROPHYTES", 1));
                as.add(new eQHEI_AttributeIMP("LOGS OR WOODY DEBRIS", 1));
                
                
                //Attribute Set Amount Attributes
                as = s1.get("Amount");
                as.add(new eQHEI_AttributeIMP("EXTENSIVE", 11));
                as.add(new eQHEI_AttributeIMP("MODERATE", 7));
                as.add(new eQHEI_AttributeIMP("SPARSE", 3));
                as.add(new eQHEI_AttributeIMP("NEARLY ABSENT", 3));
        
        //Section s2 Attribute Sets
        s2.add(new eQHEI_AttributeSetIMP("Sinuosity"));
        s2.add(new eQHEI_AttributeSetIMP("Development"));
        s2.add(new eQHEI_AttributeSetIMP("Channelization"));
        s2.add(new eQHEI_AttributeSetIMP("Stability"));
        
                //Attribute Set Sinuosity Attributes
                as = s2.get("Sinuosity");
                as.add(new eQHEI_AttributeIMP("HIGH", 4));
                as.add(new eQHEI_AttributeIMP("MODERATE", 3));
                as.add(new eQHEI_AttributeIMP("LOW", 2));
                as.add(new eQHEI_AttributeIMP("NONE", 1));
                
                //Attribute Set Development Attributes
                as = s2.get("Development");
                as.add(new eQHEI_AttributeIMP("EXCELLENT", 7));
                as.add(new eQHEI_AttributeIMP("GOOD", 5));
                as.add(new eQHEI_AttributeIMP("FAIR", 3));
                as.add(new eQHEI_AttributeIMP("POOR", 1));
                
                //Attribute Set Channelization Attributes
                as = s2.get("Channelization");
                as.add(new eQHEI_AttributeIMP("NONE", 6));
                as.add(new eQHEI_AttributeIMP("RECOVERED", 4));
                as.add(new eQHEI_AttributeIMP("RECOVERING", 3));
                as.add(new eQHEI_AttributeIMP("RECENT OR NO RECOVERY", 1));
                
                //Attribute Set Stability Attributes
                as = s2.get("Stability");
                as.add(new eQHEI_AttributeIMP("HIGH", 3));
                as.add(new eQHEI_AttributeIMP("MODERATE", 2));
                as.add(new eQHEI_AttributeIMP("LOW", 1));
                
        //Section s3 Attribute Sets
        s3.add(new eQHEI_AttributeSetIMP("Erosion"));
        s3.add(new eQHEI_AttributeSetIMP("Riparian Width"));
        s3.add(new eQHEI_AttributeSetIMP("Flood Plain Quality"));
        
                //Attribute Set Erosion Attributes
                as = s3.get("Erosion");
                as.add(new eQHEI_AttributeIMP("NONE / LITTLE", 3));
                as.add(new eQHEI_AttributeIMP("MODERATE", 2));
                as.add(new eQHEI_AttributeIMP("HEAVY / SEVERE", 1));
                
                //Attribute Set Riparian Width Attributes
                as = s3.get("Riparian Width");
                as.add(new eQHEI_AttributeIMP("WIDE > 50m", 4));
                as.add(new eQHEI_AttributeIMP("MODERATE 10-50m", 3));
                as.add(new eQHEI_AttributeIMP("NARROW 5-10m", 2));
                as.add(new eQHEI_AttributeIMP("VERY NARROW < 5m", 1));
                as.add(new eQHEI_AttributeIMP("NONE", 0));
                
                //Attribute Set Flood Plain Quality Attributes
                as = s3.get("Flood Plain Quality");
                as.add(new eQHEI_AttributeIMP("FOREST, SWAMP", 3));
                as.add(new eQHEI_AttributeIMP("SHRUB OR OLD FIELD", 2));
                as.add(new eQHEI_AttributeIMP("RESIDENTIAL, PARK, NEW FIELD", 1));
                as.add(new eQHEI_AttributeIMP("FENCED PASTURE", 1));
                as.add(new eQHEI_AttributeIMP("OPEN PASTURE, ROWCROP", 0));
                as.add(new eQHEI_AttributeIMP("CONSERVATION TILLAGE", 1));
                as.add(new eQHEI_AttributeIMP("URBAN OR INDUSTRIAL", 0));
                as.add(new eQHEI_AttributeIMP("MINING / CONSTRUCTION", 0));

        
        //Section s4 Attribute Sets
        s4.add(new eQHEI_AttributeSetIMP("Maximum Depth"));
        s4.add(new eQHEI_AttributeSetIMP("Channel Width"));
        s4.add(new eQHEI_AttributeSetIMP("Current Velocity"));
        
                //Attribute Set Maximum Depth Attributes
                as = s4.get("Maximum Depth");
                as.add(new eQHEI_AttributeIMP("> 1m", 6));
                as.add(new eQHEI_AttributeIMP("0.7 - < 1m", 4));
                as.add(new eQHEI_AttributeIMP("0.4 - < 0.7m", 2));
                as.add(new eQHEI_AttributeIMP("0.2 - < 0.4m", 1));
                as.add(new eQHEI_AttributeIMP("< 0.2m", 0));
                
                //Attribute Set Channel Width Attributes
                as = s4.get("Channel Width");
                as.add(new eQHEI_AttributeIMP("POOL WIDTH > RIFFLE WIDTH", 2));
                as.add(new eQHEI_AttributeIMP("POOL WIDTH = RIFFLE WIDTH", 1));
                as.add(new eQHEI_AttributeIMP("POOL WIDTH < RIFFLE WIDTH", 0));
                
                //Attribute Set Channel Width Attributes
                as = s4.get("Current Velocity");
                as.add(new eQHEI_AttributeIMP("TORRENTIAL", -1));
                as.add(new eQHEI_AttributeIMP("VERY FAST", 1));
                as.add(new eQHEI_AttributeIMP("FAST", 1));
                as.add(new eQHEI_AttributeIMP("MODERATE", 1));
                as.add(new eQHEI_AttributeIMP("SLOW", 1));
                as.add(new eQHEI_AttributeIMP("INTERSTITIAL", -1));
                as.add(new eQHEI_AttributeIMP("INTERMITTENT", -2));
                as.add(new eQHEI_AttributeIMP("EDDIES", 1));
                
        //Section s5 Attribute Sets
        s5.add(new eQHEI_AttributeSetIMP("Riffle Depth"));
        s5.add(new eQHEI_AttributeSetIMP("Run Depth"));
        s5.add(new eQHEI_AttributeSetIMP("Riffle / Run Substrate"));
        s5.add(new eQHEI_AttributeSetIMP("Riffle / Run Embeddedness"));
        
                //Attribute Set Riffle Depth Attributes
                as = s5.get("Riffle Depth");
                as.add(new eQHEI_AttributeIMP("BEST AREAS > 10cm", 2));
                as.add(new eQHEI_AttributeIMP("BEST AREAS 5-10cm", 1));
                as.add(new eQHEI_AttributeIMP("BEST AREAS < 5cm", 0));
                
                //Attribute Set Run Depth Attributes
                as = s5.get("Run Depth");
                as.add(new eQHEI_AttributeIMP("MAXIMUM > 50cm", 2));
                as.add(new eQHEI_AttributeIMP("MAXIMUM < 50cm", 1));
                
                //Attribute Set Riffle / Run Substrate Attributes
                as = s5.get("Riffle / Run Substrate");
                as.add(new eQHEI_AttributeIMP("STABLE", 2));
                as.add(new eQHEI_AttributeIMP("MOD. STABLE", 1));
                as.add(new eQHEI_AttributeIMP("UNSTABLE", 0));
                
                //Attribute Set Riffle / Run Embeddedness Attributes
                as = s5.get("Riffle / Run Embeddedness");
                as.add(new eQHEI_AttributeIMP("NONE", 2));
                as.add(new eQHEI_AttributeIMP("LOW", 1));
                as.add(new eQHEI_AttributeIMP("MODERATE", 0));
                as.add(new eQHEI_AttributeIMP("EXTENSIVE", -1));
        
        //Section s6 Attribute Sets
        s6.add(new eQHEI_AttributeSetIMP("Drainage Area"));
        s6.add(new eQHEI_AttributeSetIMP("Miscellaneous"));
        
                //Attribute Set Drainage Attributes
                as = s6.get("Drainage Area");
                as.add(new eQHEI_AttributeIMP("VERY LOW", 4));
                as.add(new eQHEI_AttributeIMP("MODERATE", 6));
                as.add(new eQHEI_AttributeIMP("HIGH", 10));
                        
                //Attribute Set Miscellaneous Attributes
                as = s6.get("Miscellaneous");       
                as.add(new eQHEI_AttributeIMP("% POOL", 0));
                as.add(new eQHEI_AttributeIMP("% GLIDE", 0));
                as.add(new eQHEI_AttributeIMP("% RUN", 0));
                as.add(new eQHEI_AttributeIMP("% RIFFLE", 0));
        
        return e;
    }
    
    private eQHEI_Header getHeader() throws Exception{
        //Creating Header
        eQHEI_Header h = new eQHEI_HeaderIMP();
        
        h.setDate("1/1/2014");
        h.setStreamReach(new eQHEI_StreamReachIMP(new eQHEI_UTMIMP("12N 45S 56E"), "Manchester Stream 1"));
        h.setUsername("BMBolen");
        
        return h;
    }
    
    private eQHEI_Evaluation sendEvaluation(HttpServletRequest req) throws Exception{
        //Creating Eval
        eQHEI_Evaluation e = (eQHEI_Evaluation)req.getSession().getAttribute("Eval");
        
        for(int i = 0; i < e.length(); i++){
            //Grabing the Sections
            eQHEI_Section s = e.get(i);
            
            for(int k = 0; k<s.length(); k++){
                //Grabing the Attribute Sets
                eQHEI_AttributeSet as = s.get(k);
                
                for(int j = 0; j<as.length(); j++){
                    //Grabbing the Attributes
                    eQHEI_Attribute a = as.get(j);
                    
                    //Setting Request
                    Integer count = Integer.parseInt(req.getParameter(a.getName()+ "_" + as.getName()));
                    
                    //Resetting count
                    while(count>0){
                        a.increment();
                        count--;
                    }
                }
            }
        }
        
        return e;
    }
    
    private eQHEI_Header sendHeader(HttpServletRequest req) throws Exception{
        eQHEI_Header h = new eQHEI_HeaderIMP();
        
        h.setDate(req.getParameter("date"));
        h.setUsername(req.getParameter("username"));
        h.setUTM(new eQHEI_UTMIMP(req.getParameter("UTM")));
        h.setStreamReach(new eQHEI_StreamReachIMP(new eQHEI_UTMIMP(req.getParameter("UTM")), req.getParameter("stream")));
                
        return h;
    }
}
